package com.zhangzeqin.code.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class RegisterView extends JFrame  {

    public static void main(String[] args) {
        new RegisterView();
    }
    JLabel nameLabel=new JLabel("扇贝单词注册页面",JLabel.CENTER);
    SpringLayout springLayout=new SpringLayout();
    JPanel centerPanel=new JPanel(springLayout);
    JLabel userNameLabel=new JLabel("用户名:");
    JTextField userTxt=new JTextField();
    JLabel passWordLabel=new JLabel("密码:");
    JTextField passwordField=new JTextField();
    JButton registerButton=new JButton("确定注册");

    LoginHandler loginHandler;
    RegisterHandler registerHandler;

    public RegisterView(){
        super("扇贝单词项目");
        registerHandler = new RegisterHandler(this);
        //获取内容面板
        Container contentPane=getContentPane();
        nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
        nameLabel.setPreferredSize(new Dimension(0,80));
        Font  centerFont=new Font("楷体",Font.PLAIN,20);
        userNameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200,30));
        passWordLabel.setFont(centerFont);
        passwordField.setPreferredSize(new Dimension(200,30));
        registerButton.setFont(centerFont);

        //把组件加入面板
        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(passWordLabel);
        centerPanel.add(passwordField);
        registerButton.addActionListener(loginHandler);

        //增加按钮事件
        centerPanel.add(registerButton);
        registerButton.addActionListener(registerHandler);
        //弹簧布局
        layout(contentPane);

        //自定义图标
        URL imgUrl=LoginView.class.getClassLoader().getResource("image.png");
        setIconImage(new ImageIcon(imgUrl).getImage());

        //窗口大小
        setSize(600,400);
        //计算位置居中
        setLocationRelativeTo(null);
        //关闭退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        //可视
        setVisible(true);
    }
    /**
     * 弹簧布局
     * @param contentPane
     */
    private void layout(Container contentPane) {
        //布局userNameLabel
        Spring childWidth=Spring.sum(Spring.sum(Spring.width(userNameLabel),Spring.width(userTxt)),
                Spring.constant(20));
        int offSetX=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offSetX,
                SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
        //userTxt
        springLayout.putConstraint(SpringLayout.WEST,userTxt,20,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,userTxt,0,SpringLayout.NORTH,userNameLabel);
        //passWordLabel
        springLayout.putConstraint(SpringLayout.EAST,passWordLabel,0,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,passWordLabel,20,SpringLayout.SOUTH,userNameLabel);
        //passWordField
        springLayout.putConstraint(SpringLayout.WEST,passwordField,20,SpringLayout.EAST,passWordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,passwordField,0,SpringLayout.NORTH,passWordLabel);
        //loginButton
        springLayout.putConstraint(SpringLayout.WEST,registerButton,70,SpringLayout.WEST,passWordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,registerButton,20,SpringLayout.SOUTH,passWordLabel);


        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
    }

    public JLabel getNameLabel() {
        return nameLabel;
    }

    public void setNameLabel(JLabel nameLabel) {
        this.nameLabel = nameLabel;
    }

    public SpringLayout getSpringLayout() {
        return springLayout;
    }

    public void setSpringLayout(SpringLayout springLayout) {
        this.springLayout = springLayout;
    }

    public JPanel getCenterPanel() {
        return centerPanel;
    }

    public void setCenterPanel(JPanel centerPanel) {
        this.centerPanel = centerPanel;
    }

    public JLabel getUserNameLabel() {
        return userNameLabel;
    }

    public void setUserNameLabel(JLabel userNameLabel) {
        this.userNameLabel = userNameLabel;
    }

    public JTextField getUserTxt() {
        return userTxt;
    }

    public void setUserTxt(JTextField userTxt) {
        this.userTxt = userTxt;
    }

    public JLabel getPassWordLabel() {
        return passWordLabel;
    }

    public void setPassWordLabel(JLabel passWordLabel) {
        this.passWordLabel = passWordLabel;
    }

    public JTextField getPasswordField() {
        return passwordField;
    }

    public void setPasswordField(JTextField passwordField) {
        this.passwordField = passwordField;
    }

    public JButton getRegisterButton() {
        return registerButton;
    }

    public void setRegisterButton(JButton registerButton) {
        this.registerButton = registerButton;
    }

    public LoginHandler getLoginHandler() {
        return loginHandler;
    }

    public void setLoginHandler(LoginHandler loginHandler) {
        this.loginHandler = loginHandler;
    }
}
